﻿--周在线获取绑定元宝奖励实现

--#include "data\language\LangCode.txt" once
--#include "data\config\welfare\WeekTimeBindGoldReward.lua" once


local WeekTimeBindGoldSystem = {}



--登录处理   
EventCallDispatcher._NoviceLoginMoneyReward = function ( sysarg )

    local time = System.getCurrMiniTime()

    local var = Actor.getStaticVar( sysarg )       --获得上次剩余的在线奖励记录
    local weekBindGoldReward = var.weekBindGoldReward      --在线领取绑定元宝静态变量

    if ( weekBindGoldReward == nil ) then
        var.weekBindGoldReward = {}
        weekBindGoldReward = var.weekBindGoldReward
        weekBindGoldReward[1] = 0  --在线累计时间
        weekBindGoldReward[2] = time  --记录登录初始时间和下线时间
        weekBindGoldReward[3] = 0  --上周领取的绑定元宝
        weekBindGoldReward[4] = time  --上次领取的时间
        weekBindGoldReward[5] = 0
    end

    if weekBindGoldReward[1] == nil then
        weekBindGoldReward[1] = 0
    end

    if weekBindGoldReward[2] == nil then
        weekBindGoldReward[2] = time
    end

    if System.isSameWeek(weekBindGoldReward[2], time)  then
        weekBindGoldReward[2] = time
    else

        local days = System.getDaysSinceOpenServer()

        local weeks = math.ceil(days/7)

        if weeks > #WeekTimeBindGoldRewardCfg.weekPerReward then
            weeks = #WeekTimeBindGoldRewardCfg.weekPerReward
        elseif weeks == 0 then
            weeks = 1
        end
        local perReward = WeekTimeBindGoldRewardCfg.weekPerReward[weeks]
        local currentTime = math.floor(weekBindGoldReward[1] / 3600)
        if currentTime > WeekTimeBindGoldRewardCfg.weekMaxTime then
            currentTime =  WeekTimeBindGoldRewardCfg.weekMaxTime
        end

        weekBindGoldReward[3] = currentTime * perReward
        weekBindGoldReward[1] = 0  --在线累计时间
        weekBindGoldReward[2] = time

        weekBindGoldReward[5] = 1
    end

end


--在线送元宝奖励的情况的下线存储
--下线处理 
EventCallDispatcher._LogoutRecordMoneyAwardTime = function ( sysarg )
  
  local time = System.getCurrMiniTime()
  local var = Actor.getStaticVar( sysarg )
  local weekBindGoldReward = var.weekBindGoldReward  --获得上次剩余的在线奖励记录
  if weekBindGoldReward ~= nil then
        if not weekBindGoldReward[2] then
            weekBindGoldReward[2] = time 
        end
        if not weekBindGoldReward[1] then
            weekBindGoldReward[1] = 0
        end
      --记录累计的奖励时间供下次上线用
      if (time - weekBindGoldReward[2]) > 0 then
        weekBindGoldReward[1] = weekBindGoldReward[1] + tonumber(time - weekBindGoldReward[2])
      end
      weekBindGoldReward[2] = time     --记录本次下线时间供下次上线用
  end
end


--跨日处理  --跨日由服务端自动下发
EventCallDispatcher._NewDayMoneyReward = function ( sysarg )

    local time = System.getCurrMiniTime()

    local var = Actor.getStaticVar( sysarg )       --获得上次剩余的在线奖励记录
    local weekBindGoldReward = var.weekBindGoldReward      --在线领取绑定元宝静态变量

    if ( weekBindGoldReward == nil ) then
        var.weekBindGoldReward = {}
        weekBindGoldReward = var.weekBindGoldReward
        weekBindGoldReward[1] = 0  --在线累计时间
        weekBindGoldReward[2] = time  --记录登录初始时间和下线时间
        weekBindGoldReward[3] = 0  --上周领取的绑定元宝
        weekBindGoldReward[4] = time  --上次领取的时间
        weekBindGoldReward[5] = 0
    end

    if weekBindGoldReward[1] == nil then
        weekBindGoldReward[1] = 0
    end

    if weekBindGoldReward[2] == nil then
        weekBindGoldReward[2] = time
    end

    if System.isSameWeek(weekBindGoldReward[2], time)  then
        if (time - weekBindGoldReward[2]) > 0 then
            weekBindGoldReward[1] = weekBindGoldReward[1] + tonumber(time - weekBindGoldReward[2])
        end
        weekBindGoldReward[2] = time
    else

        local days = System.getDaysSinceOpenServer()

        local weeks = math.ceil(days/7)

        if weeks > #WeekTimeBindGoldRewardCfg.weekPerReward then
            weeks = #WeekTimeBindGoldRewardCfg.weekPerReward
        elseif weeks == 0 then
            weeks = 1
        end
        local perReward = WeekTimeBindGoldRewardCfg.weekPerReward[weeks]

        local currentTime = math.floor(weekBindGoldReward[1] / 3600)
        if currentTime > WeekTimeBindGoldRewardCfg.weekMaxTime then
            currentTime =  WeekTimeBindGoldRewardCfg.weekMaxTime
        end

        weekBindGoldReward[3] = currentTime * perReward
        weekBindGoldReward[1] = 0  --在线累计时间
        weekBindGoldReward[2] = time

        weekBindGoldReward[5] = 1
    end

    SendWeekBindGold(sysarg)
end 

function SendWeekBindGold(sysarg)
    local var = Actor.getStaticVar( sysarg )       --获得上次剩余的在线奖励记录
    local weekBindGoldReward = var.weekBindGoldReward      --在线领取绑定元宝静态变量
    local time = System.getCurrMiniTime()

    if ( weekBindGoldReward == nil ) then
        var.weekBindGoldReward = {}
        weekBindGoldReward = var.weekBindGoldReward
        weekBindGoldReward[1] = 0  --在线累计时间
        weekBindGoldReward[2] = time  --记录登录初始时间和下线时间
        weekBindGoldReward[3] = 0  --上周领取的绑定元宝
        weekBindGoldReward[4] = time  --上次领取的时间
        weekBindGoldReward[5] = 0
    end

    if weekBindGoldReward[5] == nil then
        weekBindGoldReward[5] = 0
        if System.isSameWeek(weekBindGoldReward[4], time) then
            weekBindGoldReward[5] = 1
        end
    end


    --服务端返回在线信息 --跨日由服务端自动下发 
    local npack = DataPack.allocPacket(sysarg,138,LogicCmd.trSys.sub.sGetWeekOnlineBindGoldInfo)  --申请一个数据包
    if (npack == nil) then
        --print("npack:nil")
        return
    end
    local state = 0
    if weekBindGoldReward[5] == 0 then
        state = 1
    end

    local days = System.getDaysSinceOpenServer()

    local weeks = math.ceil(days/7)

    if weeks > #WeekTimeBindGoldRewardCfg.weekPerReward then
        weeks = #WeekTimeBindGoldRewardCfg.weekPerReward
    elseif weeks == 0 then
        weeks = 1
    end
    local perReward = WeekTimeBindGoldRewardCfg.weekPerReward[weeks]

    local currentTime = math.floor(weekBindGoldReward[1] / 3600)
    local currentValue
    if currentTime > WeekTimeBindGoldRewardCfg.weekMaxTime then
        currentTime =  WeekTimeBindGoldRewardCfg.weekMaxTime
    end
    currentValue = currentTime * perReward
    DataPack.writeUInt(npack,weekBindGoldReward[3])    --上周领取元宝数量
    DataPack.writeByte(npack,state)                    --上周领取元宝状态
    DataPack.writeByte(npack,currentTime)    --本周在线时间
    DataPack.writeUInt(npack,currentValue)  --累计累积元宝数量
    DataPack.flush(npack)   --发送数据包
end

WeekTimeBindGoldSystem.getWeekOnlineBindGoldReward = function(sysarg)
  
    --local level = Actor.getIntProperty( sysarg, PROP_CREATURE_LEVEL )
    if not Actor.checkActorLevel(sysarg, WeekTimeBindGoldRewardCfg.level, 0) then
        Actor.sendTipmsg( sysarg,Lang.ScriptTips.x00070,ttDialog )
        return
    end

    local time = System.getCurrMiniTime()       --获取当前的mini时间，单位秒
    local var = Actor.getStaticVar( sysarg )
    local weekBindGoldReward = var.weekBindGoldReward      

    if ( weekBindGoldReward == nil ) then
        var.weekBindGoldReward = {}
        weekBindGoldReward = var.weekBindGoldReward
        weekBindGoldReward[1] = 0  --在线累计时间
        weekBindGoldReward[2] = time  --记录登录初始时间和下线时间
        weekBindGoldReward[3] = 0  --上周领取的绑定元宝
        weekBindGoldReward[4] = time  --上次领取的时间
        weekBindGoldReward[5] = 0
    end

    if weekBindGoldReward[3] <= 0 then
        Actor.sendTipmsg(sysarg,Lang.ScriptTips.WeekBindGoldReward001,ttFlyTip) 
        return
    end

    if weekBindGoldReward[5] == nil then
        weekBindGoldReward[5] = 0
        if System.isSameWeek(weekBindGoldReward[4], time) then
            weekBindGoldReward[5] = 1
        end
    end

    if weekBindGoldReward[5] == 0 then
        Actor.sendTipmsg(sysarg,Lang.ScriptTips.WeekBindGoldReward002,ttFlyTip) 
        return
    else
        weekBindGoldReward[4] = time
        weekBindGoldReward[5] = 0
        Actor.changeMoney( sysarg, mtBindYuanbao, weekBindGoldReward[3],GameLog.clOnlineAwardMoney,Lang.LogTips.l00047)

        local str = string.format(Lang.ScriptTips.x00072, weekBindGoldReward[3])
        Actor.sendTipmsg( sysarg,str,ttDialog )
    end

    SendWeekBindGold(sysarg)
end


--周在线绑定元宝消息分配
WeekTimeBindGoldSystem.MessageHandle = function (sysarg, systemid, msgid, packet)
  if msgid == LogicCmd.trSys.sub.cGetWeekOnlineBindGoldInfo then     
    SendWeekBindGold(sysarg)
  elseif msgid == LogicCmd.trSys.sub.cGetWeekOnlineBindGoldAward then  
    WeekTimeBindGoldSystem.getWeekOnlineBindGoldReward(sysarg)
  end
end

SystemHandlerDispatcher.registerSystemHander(BaseTypes.SystemId.timeRewardSystem, WeekTimeBindGoldSystem.MessageHandle)

EventCallDispatcher.registerEventCall(BaseTypes.ActorEventId.aeUserLogin, EventCallDispatcher._NoviceLoginMoneyReward)
EventCallDispatcher.registerEventCall(BaseTypes.ActorEventId.aeUserLogout, EventCallDispatcher._LogoutRecordMoneyAwardTime)
EventCallDispatcher.registerEventCall(BaseTypes.ActorEventId.aeNewDayArrive, EventCallDispatcher._NewDayMoneyReward)